﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Common.WebSocketModule;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace DFGK_WMS.WebAPI.Controllers.PDA.出库单管理
{
    /// <summary>
    /// 出库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PDA")]
    public class ShippingOrderDetailsController : BaseController
    {
        private readonly IShippingOrderDetailService shippingOrderDetailService;
        private readonly IPickingOrderRepository pickingOrderRepository;
        private readonly WebSocketConnectManager wsManager;
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="shippingOrderDetailService"></param>
        /// <param name="pickingOrderRepository"></param>
        /// <param name="wsManager"></param>
        public ShippingOrderDetailsController(IWebHostEnvironment environment, IShippingOrderDetailService shippingOrderDetailService, IPickingOrderRepository pickingOrderRepository, WebSocketConnectManager wsManager) : base(environment)
        {
            this.shippingOrderDetailService = shippingOrderDetailService;
            this.pickingOrderRepository = pickingOrderRepository;
            this.wsManager = wsManager;
        }

        /// <summary>
        /// 手持出库选择出库单
        /// </summary>
        /// <param name="key"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingOrderDetailGroup(string? key, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = shippingOrderDetailService.GetShippingOrderDetailGroup(key, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 开始出库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartShippingOrderDetail(List<SubmitOutListDTO> m)
        {
            if (!m.Any())
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            string message;
            bool result = shippingOrderDetailService.StartShippingOrderDetail(m, out message);
            if (result)
            {
                List<PutPickQuestDto> putPickQuestDtoslist = new List<PutPickQuestDto>();
                var picklist = pickingOrderRepository.GetEntityWhereAll(x => x.Status == 4);
                if (picklist.Any())
                {
                    foreach (var dto in picklist)
                    {
                        PutPickQuestDto putPickQuestDto = new PutPickQuestDto();
                        putPickQuestDto.Code = dto.Code;
                        if (dto.BillType == 1)
                            putPickQuestDto.BillType = "出库";
                        else
                            putPickQuestDto.BillType = "拣选出库";
                        putPickQuestDto.PalletBarcode = dto.PalletBarcode;
                        putPickQuestDto.LocationCode = dto.LocationCode;
                        putPickQuestDto.BatchNo = dto.BatchNo;
                        putPickQuestDto.Qty = dto.Qty;
                        putPickQuestDto.MaterialCode = dto.MaterialCode;
                        putPickQuestDtoslist.Add(putPickQuestDto);
                    }
                }
                string liststring = JsonConvert.SerializeObject(putPickQuestDtoslist);
                wsManager.SendMsgToAllClients(liststring);//给全部连上的客户端发
                return Ok(BaseResult.Result(result, "操作成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }

        /// <summary>
        /// 拣选-开始出库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartPickingShippingOrderDetail(StartInventoryDTO m)
        {
            if (m==null)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            string message;
            bool result = shippingOrderDetailService.StartPickingShippingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 拣选出库查询数据
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingRoderData(string PalletCode)
        {
            if (string.IsNullOrEmpty(PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            var obj = shippingOrderDetailService.GetReceivingRoderData(PalletCode, out string message);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！" + message, "500", 0));
        }

        /// <summary>
        /// 拣选入库呼叫库存中箱体
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartShippingOutZeroQty(List<SubmitOutListDTO> m)
        {
            if (!m.Any())
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            string message;
            bool result = shippingOrderDetailService.StartShippingOutZeroQty(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "操作成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }

    }
}
